package com.cyq.day07;

/*
数组工具类
 */
public class ArrayUtil {
    /**
     * 将指定的元素增加到数组的最后
     *
     * @param ary
     * @param value
     * @return
     */
    public static int[] add(int[] ary, int value) {
        int[] resultAry = new int[ary.length + 1];
        for (int i = 0; i < ary.length; i++) {
            resultAry[i] = ary[i];
        }
        resultAry[resultAry.length-1] = value;
        return resultAry;
    }

    /**
     * 将数组中指定下标的元素删除
     *
     * @param ary
     * @param index
     * @return
     */
    public static int[] delete(int[] ary, int index) {
        int[] resultAry = new int[ary.length - 1];
        for (int i = 0; i < resultAry.length; i++) {
            if (i < index - 1) {
                resultAry[i] = ary[i];
            } else {
                resultAry[i] = ary[i + 1];
            }
        }
        return resultAry;
    }

    /**
     * 对指定数组进行去重
     *
     * @param ary
     * @return
     */
    public static int[] distinct(int[] ary) {
        int[] tempAry = new int[ary.length];
        int index = 0;
        for (int i = 0; i < ary.length; i++) {
            boolean flag = false;
            for (int j = 0; j < index; j++) {
                if (ary[i] == ary[j]) {
                    flag = true;
                    break;
                }
            }
            if (!flag) {
                tempAry[index] = ary[i];
                index++;
            }
        }
        int[] resultAry = new int[index];
        for (int i = 0; i < index; i++) {
            resultAry[i] = tempAry[i];
        }
        return resultAry;
    }

}
